//
//  FlickrHelper.m
//  FlickrSearching
//
//  Created by HOCVIEN on 4/25/15.
//  Copyright (c) 2015 HOCVIEN. All rights reserved.
//

#import "FlickrHelper.h"

@implementation FlickrHelper
+ (void)getTopicsPlace:(void (^) (NSArray* result)) callback{
    NSString *urlstring = @"https://api.flickr.com/services/rest/?method=flickr.places.getTopPlacesList&api_key=f45b1abe259eee0c944adbe1cd2a8aed&place_type_id=12&format=json&nojsoncallback=1";
    NSURL *urllink = [NSURL URLWithString:urlstring];
    NSURLRequest *request = [NSURLRequest requestWithURL:urllink];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError==nil) {
            NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSArray *arrPlaces = dictionary[@"places"][@"place"];
            if (callback) {
                callback(arrPlaces);
            }
        }
    }];
    
}
+ (void)getFavoriteWithPhotoId:(NSString*) photoId callback:(void (^) (NSArray* result, NSError *err)) callback{
    NSString* favoriteApi = [NSString stringWithFormat:@"https://api.flickr.com/services/rest/?method=flickr.photos.getFavorites&api_key=b5d694a91e3d14e972db299d827bdaf4&photo_id=%@&format=json&nojsoncallback=1",photoId];
    
    NSURL* urlStr = [[NSURL alloc]initWithString:favoriteApi];
    NSURLRequest* urlReq = [[NSURLRequest alloc]initWithURL:urlStr];
    [NSURLConnection sendAsynchronousRequest:urlReq queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
       
        if (!connectionError) {
            NSError* error;
            NSDictionary* apiDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
            
            if (callback) {
                NSArray* apiArray = apiDic[@"photo"][@"person"];
                callback(apiArray, error);
            }
            
        }
        else{
            if (callback) {
                callback(nil, connectionError);
            }
        }
        
        
    }];
    
}
+ (void)getPhotosWithTopPlaceId:(NSString*) topplaceId callback:(void (^) (NSArray* result)) callback{
    
    
}
+ (void)getPhotosWithUserid:(NSString*)userId callback:(void (^) (NSArray* result)) callback{
    
}
+ (void)getPhotosSetWithUserId:(NSString*)userId callback:(void (^) (NSArray* result)) callback{
    NSString* jsonUrlStr = [NSString stringWithFormat:@"https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=b5d694a91e3d14e972db299d827bdaf4&user_id=%@&extras=url_t&format=json&nojsoncallback=1&auth_token=72157650052270863-6cbaf53e33728495&api_sig=724fb5e8dff8ba816b18fc540c2a748c",userId];
    NSLog(@"testcode");
    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:jsonUrlStr]];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]  completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (!connectionError) {
            NSError* error = nil;
            NSDictionary* jsonParser = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
            NSDictionary* photos = [jsonParser objectForKey:@"photos"];
            NSArray* photo = [photos objectForKey:@"photo"];
            NSLog(@"%@",photo);
            dispatch_async(dispatch_get_main_queue(), ^{
                if (callback) {
                    callback(photo);
                }
                return;
            });
        }
    }];
}
+ (void)getPhotosWithPhotosSetId:(NSString*)photosetId callback:(void (^) (NSArray* result)) callback{
    
}
+ (void)getCommentsWithPhotosSetId:(NSString*)photosetId callback:(void (^) (NSArray* result, NSError *error)) callback{
    NSURL *url = [NSURL URLWithString:@"https://api.flickr.com/services/rest/?method=flickr.photosets.comments.getList&api_key=b5d694a91e3d14e972db299d827bdaf4&photoset_id=72157623243981334&format=json&nojsoncallback=1&api_sig=72f86a9bb752a62a49e780e6b6c18eb2"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableArray *commentsList =[[NSMutableArray alloc]init];
        if (!connectionError) {
            NSDictionary *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSDictionary *comments =[array objectForKey:@"comments"];
            //if (comment.count > 0) {
            if (callback) {
                NSArray *a = [comments objectForKey:@"comment"];
                for (int i = 0; i<6; i++) {
                    for (NSDictionary *dic in a) {
                        [commentsList addObject:dic];
                    }
                }
                callback(commentsList,connectionError);
            }
        }else{
            callback(commentsList,connectionError);
        }
    }];
}
@end
